package com.itheima1.service.impl;

import com.itheima1.pojo.Member;
import com.itheima1.pojo.MemberDTO;
import com.itheima1.pojo.MemberVO;
import com.itheima1.pojo.Order;
import com.itheima1.mapper.OrderMapper;
import com.itheima1.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 博~
 * @since 2024-12-23
 */
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate; // 明确指定类型

    @Override
    public void submitOrder(MemberDTO memberDTO) {

        memberDTO.setRegTime(LocalDateTime.now());
        orderMapper.insertMember(memberDTO);
        Integer id = memberDTO.getId();
        Integer setmealId = memberDTO.getSetmealId();
        Date orderDate = memberDTO.getOrderDate();

        if (orderMapper.checkIsOrdered(id, orderDate, setmealId) > 0) {
            throw new RuntimeException("你已经预约过了");
        }

//        // 恢复并修改 checkOrderDateisOk 的调用
//        Boolean isOk = orderMapper.checkOrderDateisOk(orderDate);
//        if (isOk == null || !isOk) {
//            throw new RuntimeException("当前该日期无法预约");
//        }else if (orderMapper.checkOrderDate(orderDate) <= 0) {
//            throw new RuntimeException("预约日期已满");
//        }

        String phoneNumber = memberDTO.getTelephone(); // 获取电话号码
        String validateCodeStr = redisTemplate.opsForValue().get(phoneNumber);// 从Redis中获取验证码字符串
        //截取最后的六位
        validateCodeStr = validateCodeStr.substring(validateCodeStr.length() - 6, validateCodeStr.length());
        if (validateCodeStr != null) {
            int validateCode = Integer.parseInt(validateCodeStr); // 将字符串转换为整数
            int memberValidateCode = memberDTO.getValidateCode(); // 获取用户输入的验证码

            if (validateCode != memberValidateCode) {
                throw new RuntimeException("验证码不匹配");
            }
        } else {
            throw new RuntimeException("未找到验证码");
        }
        Order order = new Order();
        order.setMemberId(id);
        order.setOrderDate(orderDate);
        order.setOrderType("微信预约");
        order.setOrderStatus("未到诊");
        order.setSetmealId(setmealId);
        orderMapper.submitOrder(order);
        orderMapper.updateOrderSetting(orderDate);
    }

    @Override
    public void checkLogin(MemberDTO memberDTO) {

        String phoneNumber = memberDTO.getTelephone(); // 获取电话号码
        String validateCodeStr = redisTemplate.opsForValue().get(phoneNumber);// 从Redis中获取验证码字符串
        //截取最后的六位
        validateCodeStr = validateCodeStr.substring(validateCodeStr.length() - 6, validateCodeStr.length());
        if (validateCodeStr != null) {
            int validateCode = Integer.parseInt(validateCodeStr); // 将字符串转换为整数
            int memberValidateCode = memberDTO.getValidateCode(); // 获取用户输入的验证码

            if (validateCode != memberValidateCode) {
                throw new RuntimeException("验证码不匹配");
            }
        } else {
            throw new RuntimeException("未找到验证码");
        }
    }

    @Override
    public MemberVO findById(Integer id) {
        Order order = orderMapper.findById(id);
        Member member = orderMapper.getOne(order.getMemberId());
        return new MemberVO(order.getOrderType(), member.getName(), order.getOrderDate(), order.getSetmealId());
    }
}
